﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.AspNetCore.Mvc.Extensions;
using Abp.UI;
using FwProject.Comm;
using FwProject.Controllers;
using FwProject.ManagerConfig;
using FwProject.Restaurant;
using FwProject.Restaurant.Dto;
using FwProject.Settlement;
using FwProject.Users;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace FwProject.Web.Mvc.Controllers
{
    [AbpMvcAuthorize]
    public class DepositController : FwProjectControllerBase
    {
        private readonly IDepositAppService _depositAppService;
        private readonly IRestaurantUserAppService _restaurantUserAppService;
        private readonly IRestaurantAppService _restaurantAppService;
        private readonly ISettlementAccountAppService _settlementAccountAppService;
        private readonly IUserAppService _userAppService;
        private readonly IConfigInfoAppService _configInfoAppService;

        private readonly IHostingEnvironment _env;

        public DepositController(IDepositAppService depositAppService, IRestaurantUserAppService restaurantUserAppService, IRestaurantAppService restaurantAppService, IHostingEnvironment env, ISettlementAccountAppService settlementAccountAppService, IUserAppService userAppService, IConfigInfoAppService configInfoAppService)
        {
            _depositAppService = depositAppService;
            _restaurantUserAppService = restaurantUserAppService;
            _restaurantAppService = restaurantAppService;
            _settlementAccountAppService = settlementAccountAppService;
            _userAppService = userAppService;
            _configInfoAppService = configInfoAppService;
            _env = env;
        }
        #region 收押金
        /// <summary>
        /// 收押金
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public async Task<IActionResult> CollectDeposit(int pageIdx, int size, DateTime? beginTime, DateTime? endTime, DepositStatus? DepositStatus, string RestaurantName)
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;

            if (Request.IsAjaxRequest())
            {
                var model = await _depositAppService.GetCollectDepositPagedListAsync(pageIdx, size, beginTime, endTime, DepositStatus, RestaurantName);
                return PartialView("CollectDepositPartial", model);
            }
            return View();
        }

        /// <summary>
        /// 收押金导出
        /// </summary>
        public async Task<IActionResult> ExportCollectDeposit(DateTime? beginTime, DateTime? endTime, DepositStatus? DepositStatus, string RestaurantName)
        {
            var list = await _depositAppService.GetCollectDepositListAsync(beginTime, endTime, DepositType.收押金, DepositStatus, RestaurantName);

            return DoExcel<DepositDto>(list, _env);
        }
        #endregion


        /// <summary>
        /// 获取所有餐厅
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetRestaurantData()
        {
            var model = await _restaurantAppService.GetRestaurantListAsync();
            return Json(model);
        }
        /// <summary>
        /// 获取所有账户
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetSettlementAccount()
        {
            var account = await _settlementAccountAppService.GetAllSettlementAccountAsync();
            return Json(account);
        }
        /// <summary>
        /// 获取所有经办人
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetAgentPeoperData()
        {
            var model = await _userAppService.GetAllAgentPeoperAsync();
            return Json(model);
        }

        public async Task<IActionResult> Collect()
        {
            var user = await _userAppService.GetByIdUserAsync(AbpSession.UserId.Value);
            ViewBag.FactoryinfoId = user.FactoryinfoId;
            ViewBag.AgentPeoper = user.Name;
            ViewBag.AgentPeoperUserId = user.Id;
            return View();
        }

        [HttpPost]
        public async Task<JsonResult> Collect(DepositInput input)
        {
            var id = await _depositAppService.Create(input);
            return Json(id);
        }

        public IActionResult Examine(Guid id)
        {
            var model = _depositAppService.GetById(id);
            return View(model);
        }
        #region 退押金
        /// <summary>
        /// 退押金
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public async Task<IActionResult> ReturnDeposit(int pageIdx, int size, DateTime? beginTime, DateTime? endTime, DepositStatus? DepositStatus, string RestaurantName)
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;
            var model = await _depositAppService.GetReturnDepositPagedListAsync(pageIdx, size, beginTime, endTime, DepositStatus, RestaurantName);
            if (Request.IsAjaxRequest())
            {
                return PartialView("ReturnDepositPartial", model);
            }
            return View(model);
        }
        /// <summary>
        /// 退押金导出
        /// </summary>
        /// <param name="RestaurantName"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public async Task<IActionResult> ExportReturnDeposit(string RestaurantName, DateTime? beginTime, DateTime? endTime, DepositStatus? DepositStatus)
        {
            var list = await _depositAppService.GetCollectDepositListAsync(beginTime, endTime, DepositType.退押金, DepositStatus, RestaurantName);
            return DoExcel<DepositDto>(list, _env);
        }
        #endregion

        public async Task<IActionResult> GetRestaurantDeposit(Guid id)
        {
            var data = await _restaurantAppService.GetByIdAsync(id);
            return Json(data.Deposit);
        }

        public async Task<IActionResult> GetDepositRestaurantData()
        {
            var model = await _restaurantAppService.GetReturnDepositListAsync();
            return Json(model);
        }

        public async Task<IActionResult> Return()
        {
            var user = await _userAppService.GetByIdUserAsync(AbpSession.UserId.Value);
            ViewBag.FactoryinfoId = user.FactoryinfoId;
            ViewBag.AgentPeoper = user.Name;
            ViewBag.AgentPeoperUserId = user.Id;
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> Return(DepositInput input)
        {
            if (input.AgentPeoperUserId == 0)
                throw new UserFriendlyException(200,"请选择付款账户！");
            var id = await _depositAppService.CreateReturnDeposit(input);
            return Json(id);
        }

        public IActionResult GetRestaurantByDeposit(Guid Id)
        {
            return Json(_depositAppService.GetRestaurantByDeposit(Id));

        }
        /// <summary>
        /// 根据餐厅Id查看详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult RestaurantByDepositinfo(Guid Id)
        {

            var m = _depositAppService.GetRestaurantIdByDeposit(Id);
            var model = _depositAppService.GetById(m.Id);
            return View("Examine", model);
        }


        /// <summary>
        /// 押金统计
        /// </summary>
        public async Task<IActionResult> DepositStatistics(int pageIdx, int size, DateTime? beginTime, DateTime? endTime, string RestaurantName, string IsDeposit)
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;

            if (Request.IsAjaxRequest())
            {
                var model = await _restaurantUserAppService.GetDepositPagedListAsync(pageIdx, size, beginTime, endTime, RestaurantName, IsDeposit);
                return PartialView("DepositStatisticsPartial", model);
            }
            return View();
        }

        /// <summary>
        /// 押金统计导出
        /// </summary>
        public async Task<IActionResult> ExportDepositStatistics(DateTime? beginTime, DateTime? endTime, string RestaurantName, string IsDeposit)
        {
            var list = await _restaurantUserAppService.GetExportDepositListAsync(beginTime, endTime, RestaurantName, IsDeposit);
            return DoExcel<ExportDepositStatisticsDto>(list, _env);
        }


        public async Task<IActionResult> CollectDepositPrint(Guid id)
        {
            var config = await _configInfoAppService.GetListAsync();
            ViewBag.Config = config;
            var model = _depositAppService.GetById(id);

            var restaurantUser = await _restaurantUserAppService.GetOneUser(model.RestaurantId);
            ViewBag.UserName = restaurantUser.EmployeeRestauranUser.Name;
            ViewBag.Mobile = restaurantUser.EmployeeRestauranUser.Mobile;
            ViewBag.RMB = CapitalHelper.CmycurD(Convert.ToDecimal(model.Money));
            return View(model);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Edit(Guid id)
        {
            ViewBag.Id = id;
            var model = _depositAppService.GetById(id);

            decimal ModifiableAmount = await _depositAppService.GetRestaurantModifiableAmountAsync(model.RestaurantId, id, model.DepositType);

            ViewBag.ModifiableAmount = ModifiableAmount;

            return View(model);
        }

        [HttpPost]
        public async Task<JsonResult> Edit(Guid id, DepositInput input)
        {
            await _depositAppService.Update(id, input);
            return Json(id);
        }

        public async Task<IActionResult> EditStatus(Guid id)
        {
            await _depositAppService.UpdateStatus(id);

            return Redirect("/Deposit/Examine?id=" + id);
        }

        public async Task<IActionResult> EditExamineStatus(Guid id)
        {
            await _depositAppService.UpdateExamineStatus(id);

            return Redirect("/Deposit/Examine?id=" + id);
        }

        public async Task<IActionResult> EditBackStatus(Guid id)
        {
            await _depositAppService.UpdateBackStatus(id);

            return Redirect("/Deposit/Examine?id=" + id);
        }





        /// <summary>
        /// 收退统计导出
        /// </summary>
        //public async Task<IActionResult> ExportCollectAndReturn(string RestaurantName, DepositType? Type,DateTime? beginTime,DateTime? endTime)
        //{
        //    var list = await _depositAppService.GetExportListAsync(beginTime,endTime,RestaurantName,Type);
        //    return DoExcel<DepositListDto>(list,_env);
        //}
    }
}